package com.stenio.fancat.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stenio.fancat.domain.Brand;
import com.stenio.fancat.domain.Channel;
import com.stenio.fancat.domain.bo.MainChannelBO;
import com.stenio.fancat.mapper.BrandMapper;
import com.stenio.fancat.mapper.ChannelMapper;

/**
 * 首页模块服务实现类
 */
@Service
public class MainService {

	@Autowired
	private ActivityService activityService;
	@Autowired
	private BrandMapper brandMapper;
	@Autowired
	private ChannelMapper channelMapper;

	@Value("${redirectURLPrefix}")
	public String redirectURLPrefix;

	/**
	 * 获取首页面频道结果集，数据中包含频道，品牌等信息<P>
	 */
	public Map<String, Object> queryChannelBrands() {
		List<Object> list = new ArrayList<Object>();
		// 获取频道
		List<Channel> channelList = channelMapper
				.selectList(new EntityWrapper<Channel>().orderBy(Channel.CHANNEL_ID, true));
		if (null != channelList && channelList.size() > 0) {
			for (Channel c : channelList) {
				MainChannelBO mcBO = new MainChannelBO();
				mcBO.setChannelId(c.getChannelId());
				mcBO.setName(c.getChannelName());
				mcBO.setType(1);
				list.add(mcBO);
			}
		}
		// 获取品牌
		List<Brand> brandList = brandMapper.selectList(new EntityWrapper<Brand>().orderBy(Brand.BRAND_ID, true));
		if (null != brandList && brandList.size() > 0) {
			for (Brand b : brandList) {
				MainChannelBO mcBO = new MainChannelBO();
				mcBO.setBrandId(b.getBrandId());
				mcBO.setName(b.getBrandName());
				mcBO.setType(1);
				list.add(mcBO);
			}
		}
		// 末位设置更多，方便client端操作
		MainChannelBO mcBO = new MainChannelBO();
		mcBO.setName("更多");
		mcBO.setType(2);
		list.add(mcBO);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("defSelect", "1");
		result.put("list", list);
		return result;
	}

	/**
	 * 获取活动的链接地址，并记录活动统计
	 * @param url 当前请求的url,/index/send/actId/userId/
	 * @param ip ip地址
	 */
	public String queryActUrl(String url ,String ip) {
		return activityService.saveActOperation(url,ip);
	}

}
